﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Center
{
    /// <summary>
    /// 政民互动
    /// </summary>
    public class PartyMessageBox : EntityBaseOperation
    {
        /// <summary>
        /// 主题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 姓名
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 联系方式 
        /// </summary>
        public string ContactWay { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 是否公开 
        /// </summary>
        public bool IsVisible { get; set; }

        /// <summary>
        /// 所属组织ID
        /// </summary>
        public string BranchID { get; set; }
        /// <summary>
        /// 办理组织
        /// </summary>
        public string TodoBranchID { get; set; }

        /// <summary>
        /// 转办时间
        /// </summary>
        public DateTime? TodoTime { get; set; }

        /// <summary>
        /// 状态1未处理 2 已转办 4 已回复 
        /// </summary>
        public string Status { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary>
        public string Reply { get; set; }
        /// <summary>
        /// 回复时间
        /// </summary>
        public DateTime? ReplyTime { get; set; }
        /// <summary>
        /// 性质分类 (意见建议,申诉,求决,揭发控告,咨询,其他) 
        /// </summary>
        public string MessageType { get; set; }

        /// <summary>
        /// 问题分类(政治综合,经济综合,宣传舆论,纪检监察,政法,劳动社保,教育,卫生计生,科技文体,组织人事,交通能源环保,民政,农村农业,国土资源水利林业,城乡建设,信息产业,商贸旅游,其他)
        /// </summary>
        public string MessageTheme { get; set; }

        #region 数据
        public static DataSet GetGridData(int pageIndex, int pageSize,  string searchKey = "", string status = "",bool showPortal=false)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize},

            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS  a.*,m.UserName UserName2,m2.UserName as ReplyBy,
                  
                    if(a.Status=4,'已回复','待回复') as StatusName
                    from tblPartyMessageBox a
                    left join tblpartyorgmember m on (m.LoginID=a.CreateBy)
                    left join tblpartyorgmember m2 on (m2.LoginID=a.ModifyBy)
                    where 1=1 ");
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and a.Title like ?key ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.String, ParameterName = "key", Value = string.Format("%{0}%", searchKey) });
            }
            if (!string.IsNullOrEmpty(status))
            {
                sb.Append(" and a.status = ?status ");
                parameters.Add(
                    new MySqlParameter() { DbType = DbType.Int32, ParameterName = "status", Value = Convert.ToInt32(status) });
            }
            sb.Append(showPortal ? " order by a.CreateTime desc" : " order by a.Status desc, a.CreateTime desc");

            sb.Append("  LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static DataTable GetSingle(string id)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.String,ParameterName = "id",Value = id},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "CategoryDD",Value = SysDataDictionary.EduThemeCategory},
                new MySqlParameter(){DbType = DbType.String,ParameterName = "CourseTypeDD",Value = SysDataDictionary.EduCourseType}
            };
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select a.*,d1.DDicMemberName as CategoryName,d2.DDicMemberName as CourseTypeName,
                    if(a.Status=4,'已发布','草稿') as StatusName from tblPartyEduCourseware a 
left join tbldatadictionary d1 on (d1.DDicType=?CategoryDD and a.Category=d1.DDicMember)
                    left join tbldatadictionary d2 on (d2.DDicType=?CourseTypeDD and a.CourseType=d2.DDicMember)
where a.id=?id ");

            return MySqlHelper.ExecuteDataTable(sb.ToString(), parameters.ToArray());
        }
    }
}
